var numIslands = function (grid) {
    // 思路：
    // 1. 当遇到陆地时，将陆地标记为水，并继续递归遍历该陆地的相邻陆地。
    // 2. 遇到水时，跳过。
    // 3. 边界条件：i < 0 || i >= m || j < 0 || j >= n
    // 4. 递归终止条件：i < 0 || i >= m || j < 0 || j >= n || grid[i][j] === '0'
    let m = grid.length;
    let n = grid[0].length;
    let count = 0;

    function dfs(i, j) {
        if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] === '0') {
            return;
        }
        grid[i][j] = '0';
        dfs(i + 1, j);
        dfs(i - 1, j);
        dfs(i, j + 1);
        dfs(i, j - 1);
    }

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] === '1') {
                dfs(i, j);
                count++;
            }
        }
    }
    return count;
};

var numIslands = function (grid) {
    let m = grid.length, n = grid[0].length;
    let count = 0;

    function dfs(i, j) {
        if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] === '0') {
            return;
        }
        grid[i][j] = '0';
        dfs[i - 1][j];
        dfs[i + 1][j];
        dfs[i][j + 1];
        dfs[i][j - 1];
    };

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] === '1') {
                dfs[i][j];
                count++;
            }
        }
    }

    return count;
}